package com.mccken.eshop.order.api.impl;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import com.mccken.eshop.common.code.JsonResult;
import com.mccken.eshop.order.api.OrderApi;
import com.mccken.eshop.order.domain.request.CreateOrderParams;
import com.mccken.eshop.order.domain.request.GenOrderIdParams;
import com.mccken.eshop.order.domain.response.CreateOrderResult;
import com.mccken.eshop.order.domain.response.GenOrderIdResult;
import com.mccken.eshop.order.exception.OrderBizException;
import com.mccken.eshop.order.exception.OrderErrorCodeEnum;
import com.mccken.eshop.order.service.OrderService;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-08-28 16:20
 **/
@DubboService(version = "1.0.0", interfaceClass = OrderApi.class, retries = 0)
@Slf4j
public class OrderApiImpl implements OrderApi {

	@Autowired
	private OrderService orderService;

	/**
	 * 生成订单号接口
	 */
	@Override
	public JsonResult<GenOrderIdResult> genOrderId(GenOrderIdParams params) {
		try {
			String userId = params.getUserId();
			if (StrUtil.isBlank(userId)) {
				return JsonResult.buildError(OrderErrorCodeEnum.USER_ID_IS_NULL);
			}
			GenOrderIdResult genOrderIdResult = orderService.genOrderId(params);
			return JsonResult.buildSuccess(genOrderIdResult);
		} catch (OrderBizException e) {
			log.error("biz", e);
			return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
		} catch (Exception e) {
			log.error("system error", e);
			return JsonResult.buildError(e.getMessage());
		}
	}

	@Override
	public JsonResult<CreateOrderResult> createOrder(CreateOrderParams params) {
		
		try {
			CreateOrderResult result = orderService.createOrder(params);
			return JsonResult.buildSuccess(result);
		} catch (OrderBizException e) {
			log.error("biz error", e);
			return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
		} catch (Exception e) {
			log.error("system error", e);
			return JsonResult.buildError(e.getMessage());
		}
	}

}
